using System;
using Edge.Common.ComponentModel.Parameters;

namespace Edge.Common
{
    public class FetchOptions : ICloneable
    {
        private int firstResult = 0;
        private int maxResults = 1;
        private SortDirection sortDirection = SortDirection.Ascending;
        private string sortExpression = string.Empty;


        public FetchOptions()
        {
        }

        public static FetchOptions All
        {
            get
            {
                return new FetchOptions(0, 0, SortDirection.Ascending, "");
            }
        }

        public FetchOptions(int firstResult, int maxResults, SortDirection sortDirection, string sortExpression)
        {
            this.firstResult = firstResult;
            this.maxResults = maxResults;
            this.sortDirection = sortDirection;
            this.sortExpression = sortExpression;
        }

        public int FirstResult
        {
            get { return firstResult; }
            set { firstResult = value; }
        }

        public int MaxResults
        {
            get { return maxResults; }
            set { maxResults = value; }
        }

        public SortDirection SortDirection
        {
            get { return sortDirection; }
            set { sortDirection = value; }
        }

        public string SortExpression
        {
            get { return sortExpression; }
            set { sortExpression = value; }
        }


        ///<summary>
        ///Creates a new object that is a copy of the current instance.
        ///</summary>
        ///
        ///<returns>
        ///A new object that is a copy of this instance.
        ///</returns>
        ///<filterpriority>2</filterpriority>
        public object Clone()
        {
            return new FetchOptions(this.firstResult, this.maxResults, this.sortDirection, this.sortExpression);
        }


    }

    public class FetchOptionForm
    {
        private int page = 1;
        private int rows = 24;
        private string sort = "";
        private bool desc = false;


        [QueryParameter("page", IsRequired = false)]
        public int Page
        {
            get { return page; }
            set { page = value; }
        }

        [QueryParameter("rows", IsRequired = false)]
        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        [QueryParameter("sort", IsRequired = false)]
        public string Sort
        {
            get { return sort; }
            set { sort = value; }
        }

        [QueryParameter("desc", IsRequired = false)]
        public bool Desc
        {
            get { return desc; }
            set { desc = value; }
        }

        public FetchOptions Parse()
        {
            FetchOptions fo = new FetchOptions();
            fo.SortExpression = Sort;
            fo.SortDirection = desc ? SortDirection.Descending : SortDirection.Ascending;
            fo.FirstResult = (Page - 1) * Rows;
            fo.MaxResults = Rows;
            return fo;
        }
    }
}
